package com.uchedao.seller.ui.car.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.uchedao.seller.R;
import com.uchedao.seller.config.GlobalInfo;

import java.util.ArrayList;

public class AdviceAdapter extends BaseAdapter {
	String[] values = GlobalInfo.resources.getStringArray(R.array.maintenance_values);
	boolean[] checked = new boolean[values.length];

	private Context mContext;

	private ArrayList<String> list;

	public AdviceAdapter(Context context, ArrayList<String> array) {
		mContext = context;

		if (array != null) {
			boolean flag = false;
			for (int i = 0; i < values.length; i++) {
				flag = false;
				for (String text : array) {
					if (text.equals(values[i])) {
						flag = true;
						break;
					}
				}
				checked[i] = flag;
			}
		} else {
			array = new ArrayList<String>();
		}

		list = array;
	}

	/**
	 * 得到选中的
	 * 
	 * @return
	 */
	public ArrayList<String> getCheckedValues() {
		ArrayList<String> array = new ArrayList<String>();
		for (int i = 0; i < checked.length; i++) {
			if (checked[i])
				array.add(values[i]);
		}
		return array;
	}

	@Override
	public int getCount() {
		return values.length;
	}

	@Override
	public Object getItem(int position) {
		return values[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int position = (Integer) v.getTag();
			checked[position] = !checked[position];
			notifyDataSetChanged();

			if (checked[position])
				list.add(values[position]);
			else
				list.remove(values[position]);

		}
	};

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = new Button(mContext);
			convertView.setOnClickListener(onClickListener);
		}
		convertView.setTag(position);
		((Button) convertView).setText(values[position]);
		((Button) convertView).setTextSize(14);
		convertView.setBackgroundResource(checked[position] ? R.drawable.condition_maintenance_selected
				: R.drawable.condition_maintenance);
		((Button) convertView).setTextColor(checked[position] ? mContext.getResources().getColor(R.color.alert_blur): mContext.getResources().getColor(R.color.deep_green));

		return convertView;
	}

}